package com.sjz.han.industry.fragments;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjz.han.industry.R;
import com.sjz.han.industry.activites.AllTeamRadiosActivity;
import com.sjz.han.industry.activites.MemberDetailActivity;
import com.sjz.han.industry.activites.UploadPayMsgActivity;
import com.sjz.han.industry.adapters.MainTeamAdapter;
import com.sjz.han.industry.base.BaseFragment;
import com.sjz.han.industry.entities.HomeEntity;
import com.sjz.han.industry.entities.MainTeamEntity;
import com.sjz.han.industry.entities.TeamRadioEntity;
import com.sjz.han.industry.utils.PicUtils;
import com.sjz.han.industry.weight.MarqueeView;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.ArrayList;
import java.util.List;
import com.sjz.han.industry.entities.LoignEntity;
import com.sjz.han.industry.internetmanage.ReqestUrl;
import com.sjz.han.industry.utils.JsonUtils;
import com.sjz.han.industry.utils.SysUtils;
import com.sjz.han.industry.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;


/**
 * 团队
 */
public class TeamFragment extends BaseFragment implements View.OnClickListener{
    private TextView team_zhedan_tv;//遮挡
    private TextView teamf_leve_tv,teamf_personnum_tv;//等级，还差多少人晋级

    private MarqueeView home_notice_marquee;
    private List<String> pmdStringList = new ArrayList<>();
    private List<TeamRadioEntity.TeamRadioData.TeamRadio> noticeBeans=new ArrayList<>();
    private TextView team_morenotice_tv;


    private SwipeMenuRecyclerView team_recy;
    private MainTeamAdapter teamAdapter;
    private View headview=null;

    private LinearLayout team_C2HY_ll;//二级会员部分
    private ImageView teamc2_photo_img;//二级会员头像
    private TextView teamc2_name_tv,teamc2_phone_tv;//姓名，电话
    private String c2UserId="";
    private RelativeLayout teamc2_noticepart_rel;//广播部分
    private TextView teamc2_titlename_tv;//
    @Override
    protected int setLayoutId() {
        return R.layout.fragment_team;
    }

    @Override
    protected void initViews() {
        team_zhedan_tv=(TextView)getView().findViewById(R.id.team_zhedan_tv);
        headview= LayoutInflater.from(activity).inflate(R.layout.view_team_headview,null);
        teamf_leve_tv=(TextView)headview.findViewById(R.id.teamf_leve_tv);
        teamf_personnum_tv=(TextView)headview.findViewById(R.id.teamf_personnum_tv); //等级，还差多少人晋级

        teamc2_titlename_tv=(TextView)headview.findViewById(R.id.teamc2_titlename_tv);

        team_C2HY_ll=(LinearLayout)headview.findViewById(R.id.team_C2HY_ll);
        teamc2_photo_img=(ImageView)headview.findViewById(R.id.teamc2_photo_img);
        teamc2_name_tv=(TextView)headview.findViewById(R.id.teamc2_name_tv);
        teamc2_phone_tv=(TextView)headview.findViewById(R.id.teamc2_phone_tv);

        teamc2_noticepart_rel=(RelativeLayout)headview.findViewById(R.id.teamc2_noticepart_rel);
        home_notice_marquee=(MarqueeView)headview.findViewById(R.id.team_notice_marquee);
        pmdStringList=new ArrayList<>();
        team_morenotice_tv=(TextView)headview.findViewById(R.id.team_morenotice_tv);

        team_recy=(SwipeMenuRecyclerView)getView().findViewById(R.id.team_recy);
        team_recy.setLayoutManager(new LinearLayoutManager(activity));
        team_recy.addHeaderView(headview);
        teamAdapter=new MainTeamAdapter(activity);
        team_recy.setAdapter(teamAdapter);
    }

    @Override
    protected void initDatas() {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            getTeamList();
        }
    }

    @Override
    protected void setLisener() {
        home_notice_marquee.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
            @Override
            public void onItemClick(int position, TextView textView) {
//                startActivity(new Intent(activity, DY_NoticeDetailActivity.class).putExtra("id",pmdList.get(position).id));
            }
        });
        team_C2HY_ll.setOnClickListener(this);
        team_morenotice_tv.setOnClickListener(this);
    }

    @Override
    protected void onRetry() {
     getTeamList();
    }

    @Override
    public void onClick(View v) {
      switch (v.getId()){
          case R.id.team_C2HY_ll:
              startActivity(new Intent(activity, MemberDetailActivity.class).putExtra("userId",c2UserId));
              break;
          case R.id.team_morenotice_tv:
              startActivity(new Intent(activity, AllTeamRadiosActivity.class));
              break;
      }
    }

    @Override
    public void onResume() {
        super.onResume();
        guangboList();
//        1也就是普通会员，2 A级 3 B级 4 C1级 5 C2级 6 C3
        if("1".equals(shareUtils.getLevel())){
            team_C2HY_ll.setVisibility(View.GONE);
            teamc2_noticepart_rel.setVisibility(View.GONE);
            teamf_leve_tv.setText("普通会员");
            team_zhedan_tv.setVisibility(View.VISIBLE);
        }else if("2".equals(shareUtils.getLevel())){
            team_C2HY_ll.setVisibility(View.VISIBLE);
            teamc2_noticepart_rel.setVisibility(View.VISIBLE);
            team_zhedan_tv.setVisibility(View.GONE);
            teamf_leve_tv.setText("A级会员");
        }else if("3".equals(shareUtils.getLevel())){
            team_C2HY_ll.setVisibility(View.VISIBLE);
            teamc2_noticepart_rel.setVisibility(View.VISIBLE);
            team_zhedan_tv.setVisibility(View.GONE);
            teamf_leve_tv.setText("B级会员");
        }else if("4".equals(shareUtils.getLevel())){
            team_C2HY_ll.setVisibility(View.VISIBLE);
            teamc2_noticepart_rel.setVisibility(View.VISIBLE);
            team_zhedan_tv.setVisibility(View.GONE);
            teamf_leve_tv.setText("一级会员");
        }else if("5".equals(shareUtils.getLevel())){
            teamf_leve_tv.setText("二级会员");
            team_C2HY_ll.setVisibility(View.GONE);
            teamc2_noticepart_rel.setVisibility(View.VISIBLE);
            team_zhedan_tv.setVisibility(View.GONE);
        }else if("6".equals(shareUtils.getLevel())){
            teamf_leve_tv.setText("三级会员");
            team_C2HY_ll.setVisibility(View.VISIBLE);
            teamc2_noticepart_rel.setVisibility(View.VISIBLE);
            team_zhedan_tv.setVisibility(View.GONE);
        }

    }

  public void getTeamList(){
        showLoading();
        OkHttpUtils
                .post()
                .url(ReqestUrl.TEAM_Mytem_URL)
                .addParams("token",shareUtils.getToken())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.popUpToast("请求超时");
                showFaild();
            }
            @Override
            public void onResponse(String response, int id) {
                showSuccess();
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    MainTeamEntity entity= JsonUtils.getObject(response,MainTeamEntity.class);
                    if(entity!=null&&entity.code==0){
                        if("1".equals(shareUtils.getLevel())){
                            if("0".equals(shareUtils.getJhStatus())){
                                teamf_personnum_tv.setText("待激活");
                            }else{
                                teamf_personnum_tv.setText("距"+entity.data.count+"人晋级");
                            }
//                            teamf_personnum_tv.setText("距"+entity.data.user.num+"人晋级");
                        }else{
                            teamf_personnum_tv.setText(entity.data.user.nextCount+"人");
                        }

                        if(entity.data.c2user!=null){
                            c2UserId=entity.data.c2user.userId;
                            teamc2_name_tv.setText("姓名："+entity.data.c2user.username);
                            teamc2_phone_tv.setText("联系电话："+entity.data.c2user.phone);//姓名，电话
                            PicUtils.showImageViewToCircle(activity, R.mipmap.ic_wd_tx, ReqestUrl.BASE+entity.data.c2user.photo, teamc2_photo_img);

                        }
                      if(entity.data.user.nextList!=null){
                          teamAdapter.setAdaterDatas(entity.data.user.nextList);
                          if("2".equals(shareUtils.getLevel())){
                              teamc2_titlename_tv.setText("普通会员("+entity.data.user.nextList.size()+")");
                          }else if("3".equals(shareUtils.getLevel())){
                              teamc2_titlename_tv.setText("A级会员("+entity.data.user.nextList.size()+")");
                          }else if("4".equals(shareUtils.getLevel())){
                              teamc2_titlename_tv.setText("B级会员("+entity.data.user.nextList.size()+")");
                          }else if("5".equals(shareUtils.getLevel())){
                              teamc2_titlename_tv.setText("一级会员("+entity.data.user.nextList.size()+")");
                          }else if("6".equals(shareUtils.getLevel())){
                              teamc2_titlename_tv.setText("二级会员("+entity.data.user.nextList.size()+")");
                          }

                      }
                    }else{
                        ToastUtils.popUpToast(entity.msg);
                    }
                }
            }
        });
    }
    public void guangboList(){
        OkHttpUtils
                .post()
                .url(ReqestUrl.TEAM_GUANGBO_URL)
                .addParams("token",shareUtils.getToken())
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtils.popUpToast("请求超时");
            }
            @Override
            public void onResponse(String response, int id) {
                if(ReqestUrl.rebacRequestJson(response)!=null){
                    TeamRadioEntity entity= JsonUtils.getObject(response,TeamRadioEntity.class);
                    if(entity!=null&&entity.code==0){
                        if(entity.data.CRadios!=null&&entity.data.CRadios.size()>0){//广播
                            noticeBeans.clear();
                            noticeBeans.addAll(entity.data.CRadios);
                            for(int i=0;i<noticeBeans.size();i++){
                                pmdStringList.add(noticeBeans.get(i).content);
                                home_notice_marquee.startWithList(pmdStringList);
                            }
                        }
                    }else{
                        ToastUtils.popUpToast(entity.msg);
                    }
                }
            }
        });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }
}
